package com.example.wechatdaka.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechatdaka.entity.*;
import com.example.wechatdaka.service.CheckworkService;
import com.example.wechatdaka.service.ClazzService;
import com.example.wechatdaka.service.SigninService;
import com.example.wechatdaka.utils.GetQRCodeUtil;
import com.example.wechatdaka.utils.Result;
import com.example.wechatdaka.utils.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-11-30
 */
@Api(tags = "签到发布模块")
@RestController
@RequestMapping("/checkwork")
public class CheckworkController {
    @Autowired
    private CheckworkService checkworkService;
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private SigninService signinService;
    @ApiOperation(value = "分页查看所有已发布签到信息")
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public JsonResult queryAllByPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String id) {
        Page<Checkwork> page = new Page<>(currentPage, pagesize);
        IPage<Checkwork> userWechatIPage=new Page<>();
        if("1".equals(id)){
            userWechatIPage = checkworkService.queryAll(page);
        }else {
            userWechatIPage = checkworkService.queryAllByTeaId(page,id);
        }
        List<Checkwork> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "分页根据教师id查看考勤发布")
    @RequestMapping(value = "/queryCheckByTeacherPage",method = RequestMethod.GET)
    public JsonResult queryCheckClazzByTeacherPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                                   @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String id) {
        Page<Checkwork> page = new Page<>(currentPage, pagesize);
        IPage<Checkwork> userWechatIPage=new Page<>();
        if("1".equals(id)){
            userWechatIPage = checkworkService.queryAll(page);
        }else {
            userWechatIPage = checkworkService.queryAllByTeaId(page,id);
        }

        List<Checkwork> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "分页根据教师id查看所有已发布签到信息")
    @RequestMapping(value = "/queryByTeacher",method = RequestMethod.GET)
    public JsonResult queryByTeacherPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String keyword) {
        Page<Checkwork> page = new Page<>(currentPage, pagesize);
        IPage<Checkwork> userWechatIPage = checkworkService.queryByTeacherKeyword(page,keyword);
        List<Checkwork> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "分页根据考勤发布名查看所有已发布签到信息")
    @RequestMapping(value = "/queryByKeyword",method = RequestMethod.GET)
    public JsonResult queryByKeywordPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String keyword) {
        Page<Checkwork> page = new Page<>(currentPage, pagesize);
        IPage<Checkwork> userWechatIPage = checkworkService.queryByKeyword(page,keyword);
        List<Checkwork> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "删除考勤发布")
    @RequestMapping(value = "/deleteCheckwork",method = RequestMethod.POST)
    public Result deleteById(@RequestBody String id){
        QueryWrapper<Signin> signinQueryWrapper=new QueryWrapper<>();
        signinQueryWrapper.eq("lxCheckworkId",id);
        signinService.remove(signinQueryWrapper);
        boolean removeById = checkworkService.removeById(id);
        if (removeById){
            return new Result(Result.SUCCESS,null,"SUCCESS");
        }
        return new Result(Result.ERROR,null,"error");
    }
    @ApiOperation(value = "新增考勤发布")
    @RequestMapping(value = "/addCheckwork",method = RequestMethod.POST)
    public Result addCheckwork(@RequestBody Checkwork checkwork, HttpServletResponse response){
        String lxClazzId = checkwork.getLxClazzId();
        String lxClazzName="";

        String[] split = lxClazzId.split(",");
        for (int i = 0; i < split.length; i++) {
            Clazz clazz = clazzService.getById(split[i]);
            if (i==split.length-1){
                lxClazzName=lxClazzName+clazz.getLxClazzName();
            }else {
                lxClazzName=lxClazzName+clazz.getLxClazzName()+",";
            }
        }
        checkwork.setLxClazzName(lxClazzName);
        Date date=new Date();
        checkwork.setLxReleaseTime(date);
        boolean save = checkworkService.save(checkwork);
        if (save){
            String checkId=""+checkwork.getId();
            return new Result(Result.SUCCESS,checkId,checkId);
        }
        return new Result(Result.ERROR,null,"error");
    }
    @ApiOperation(value = "小程序根据id查询考勤信息")
    @RequestMapping(value = "/queryCheckworkByIdMini",method = RequestMethod.POST)
    public Result queryByIdMini(String checkId){
        Checkwork checkwork = checkworkService.queryByIdMini(checkId);
        if (checkwork!=null){
            return new Result(Result.SUCCESS,checkwork,"SUCCESS");
        }
        return new Result(Result.ERROR,null,"error");
    }
    @ApiOperation(value = "根据id停止签到")
    @RequestMapping(value = "/stopSignin",method = RequestMethod.POST)
    public Result stopSignin(@RequestBody String checkId){
        Checkwork checkwork = checkworkService.queryByIdMini(checkId);
        String[] clazzIds = checkwork.getLxClazzId().split(",");
        List<Signin> signinList=new ArrayList<>();
        for (int i = 0; i < clazzIds.length; i++) {
            List<String> stringList = checkworkService.queryUnSignByClazz(clazzIds[i], checkId);
            for (String str:stringList) {
                Signin signin=new Signin();
                signin.setLxCheckworkId(checkId);
                signin.setLxUserNumber(str);
                signin.setLxSignInStatus(4);
                signinList.add(signin);
            }
        }
        boolean saveBatch = signinService.saveBatch(signinList);
        if (saveBatch){
            return new Result(Result.SUCCESS,null,"SUCCESS");
        }
        return new Result(Result.ERROR,null,"error");
    }


}

